const id = location.search.split("?")[1];

ajax("http://localhost:3000/api",{
    type:"getDetail",
    id:id
}).then(res=>{
    res = JSON.parse(res);

    // console.log(res);
    if(res.code === 1){
        // 准备渲染
        render(res.data);
    }else if(res.code === 0){
        alert("商品卖完了");
        location.href = "http://localhost:3000/bowl.html"
    }
})


const img = document.querySelector(".five-b-a");
const zname = document.getElementById("name");
const eee = document.querySelector(".eee");
const eeee = document.querySelector(".five-g");
function render(data){
    img.src = data.img;
    zname.innerHTML = data.name;
    eee.innerHTML = data.price;
    eeee.innerHTML = data.price;
}


function ajax(url, data){
    let str = "";
    for(let i in data){
        str += `${i}=${data[i]}&`
    }
    url += "?" + str;

    const xhr = new XMLHttpRequest();
    xhr.open("get", url);
    xhr.send();

    return new Promise((resolve,reject)=>{
        xhr.onload = function(){
            if(xhr.status === 200){
                resolve(xhr.responseText);
            }else{
                reject(xhr.status);
            }
        }
    })
}